﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using CrystalDecisions;
using CrystalDecisions.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace Volunteer
{
    public partial class Reports : System.Web.UI.Page
    {
        protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
        private ReportDocument report = new ReportDocument();
        private ConnectionInfo connInfo;

        protected override void OnPreRender(EventArgs e)
        {
            //attach our report to viewer and set database login
            report.Load(Server.MapPath("VolHours.rpt"));
            connInfo = new ConnectionInfo();
            connInfo.ServerName = ConfigurationManager.AppSettings["DBServer"].ToString();
            connInfo.DatabaseName = ConfigurationManager.AppSettings["Database"].ToString();
            connInfo.UserID = ConfigurationManager.AppSettings["UserID"].ToString();
            connInfo.Password = ConfigurationManager.AppSettings["Password"].ToString();

            foreach (TableLogOnInfo cnInfo in CrystalReportViewer1.LogOnInfo)
            {
                cnInfo.ConnectionInfo = connInfo;
            }

            report.SetDatabaseLogon(connInfo.UserID, connInfo.Password, connInfo.ServerName, connInfo.DatabaseName);
            CrystalReportViewer1.ReportSource = report;

            base.OnPreRender(e);
        }

        private void Page_Init(object sender, System.EventArgs e)
        {
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            
        }
        
        protected void lnkExport_Click(object sender, EventArgs e)
        {
            MemoryStream oStream;
            oStream = (MemoryStream)
            report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(oStream.ToArray());
            Response.End();
        }

        private void ConfigureCrystalReports()
        {
        }

        protected void calDateFrom_SelectionChange(object sender, EventArgs e)
        {
            txtDateFrom.Text = calDateFrom.SelectedDate.ToString("d");
            //calDateFrom.Visible = false;
            dateFromPicker.Visible = false;
        }

        protected void calDateTo_SelectionChange(object sender, EventArgs e)
        {
            txtDateTo.Text = calDateTo.SelectedDate.ToString("d");
            //calDateTo.Visible = false;
            dateToPicker.Visible = false;
        }

        protected void lnkShow_Click(object sender, EventArgs e)
        {
            ParameterFields ParamFields = CrystalReportViewer1.ParameterFieldInfo;
            ParameterValues curVals = new ParameterValues();
            CrystalReportViewer1.ParameterFieldInfo.Clear();
            report.Load(Server.MapPath("VolHours.rpt"));

            ParameterValues pvValues = new ParameterValues();
            ParameterFieldDefinitions pfdDefinitions = this.report.DataDefinition.ParameterFields;
                
            if (txtDateFrom.Text.Length > 0)
            {
                //ParameterField dtFrom = new ParameterField();
                //dtFrom.Name = "@DateFrom";
                ParameterDiscreteValue dtFromVal = new ParameterDiscreteValue();
                dtFromVal.Value = Convert.ToDateTime(txtDateFrom.Text);
                pvValues.Add(dtFromVal);

                //Session["@DateFrom"] = txtDateFrom.Text;
                //dtFrom.CurrentValues.Add(dtFromVal);
                //ParamFields.Add(dtFrom);
                //curVals.Add(dtFromVal);
                ParameterFieldDefinition prdDefinition = pfdDefinitions["@DateFrom"];
                prdDefinition.ApplyCurrentValues(pvValues);
            }

            if (txtDateTo.Text.Length > 0)
            {
                //ParameterField dtTo = new ParameterField();
                //dtTo.Name = "@DateTo";
                ParameterDiscreteValue dtToVal = new ParameterDiscreteValue();
                dtToVal.Value = Convert.ToDateTime(txtDateTo.Text);
                pvValues.Add(dtToVal);

                //Session["@DateTo"] = txtDateTo.Text;
                //dtTo.CurrentValues.Add(dtToVal);
                //ParamFields.Add(dtTo);
                //curVals.Add(dtToVal);
                ParameterFieldDefinition prdDefinition = pfdDefinitions["@DateTo"];
                prdDefinition.ApplyCurrentValues(pvValues);
            }
            
            //foreach (ParameterField param in ParamFields)
            //{
            //    report.SetParameterValue(param.Name, Session[param.Name]);
            //}
            
            CrystalReportViewer1.RefreshReport();
        }

        protected void lnkFirst_Click(object sender, EventArgs e)
        {
            CrystalReportViewer1.ShowFirstPage();
        }

        protected void lnkPrevious_Click(object sender, EventArgs e)
        {
            CrystalReportViewer1.ShowPreviousPage();
        }

        protected void lnkNext_Click(object sender, EventArgs e)
        {
            CrystalReportViewer1.ShowNextPage();
        }

        protected void lnkLast_Click(object sender, EventArgs e)
        {
            CrystalReportViewer1.ShowLastPage();
        }
           
    }
}
